import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


import javax.imageio.ImageIO;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

import java.awt.print.*;
import java.io.IOException;
import java.util.Vector;

public class ProjectMain extends JFrame {

	  private int INIT_WINDOW_WIDTH = 877;
	  private int INIT_WINDOW_HEIGHT =  622;
	  
	  public static void main(String[] args) {
	    SwingUtilities.invokeLater(new Runnable() {
	      public void run() {
	    	ProjectMain pm = new ProjectMain();
	        pm.initGUI();
	        pm.setVisible(true);
	      }
	    });
	  }
	  
	  
		//Reference to canvas
		Canvas myCanvas;
		
	  
	  public void initGUI() {
	    
		  final Canvas bottom = new Canvas();
		    bottom.init();
		  
		//begin initiating left content
		    JTabbedPane top = new JTabbedPane(JTabbedPane.TOP);
		    top.setMinimumSize(new Dimension(100, 0));
		    Math math = new Math(bottom);
		    final Reading reading = new Reading(bottom);
		    Game game = new Game(bottom);
		    final Writing writing = new Writing(bottom);
		    math.init();
		    reading.init();
		    game.init();
		    writing.init();
		    top.addTab("Math Library", math);
		    top.addTab("Reading Library", reading);
		    top.addTab("Game Library", game);
		    top.addTab("Writing Library", writing);
		    
		  
		//begin initiating bottom content
		  
		  	JPanel container = new JPanel();
		  	SpringLayout layout = new SpringLayout();
		  	layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, bottom,
                    0,
                    SpringLayout.HORIZONTAL_CENTER, container);
		  	layout.putConstraint(SpringLayout.VERTICAL_CENTER, bottom,
                    0,
                    SpringLayout.VERTICAL_CENTER, container);
		  	
		  	container.add(bottom);
		  	
		  	//grid button
		  	JCheckBox grid = new JCheckBox("Show Grid");
		  	grid.setSelected(true);
		  	grid.addItemListener(new ItemListener(){

				@Override
				public void itemStateChanged(ItemEvent arg0) {
					if (arg0.getStateChange() == ItemEvent.DESELECTED)
					{
						bottom.setGrid(false);
					}
					else
						bottom.setGrid(true);
					
					bottom.repaint();
				}
		  	});
		  	layout.putConstraint(SpringLayout.SOUTH, grid,
                    -1,
                    SpringLayout.NORTH, bottom);
		  	layout.putConstraint(SpringLayout.EAST, grid,
                    0,
                    SpringLayout.EAST, bottom);
		  	container.add(grid);
		  	
		  
			
			//Text button
			final JButton text = new JButton();
			text.setBorder(BorderFactory.createEmptyBorder());
			text.setOpaque(true);
			text.setBackground(new Color(235,235,235));
			try {
			    Image img = ImageIO.read(getClass().getResource("resources/draw-text-icon.png"));
			    text.setIcon(new ImageIcon(img));
			  } catch (IOException ex) {
			  }
			text.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					String input, result, text, res; 
					input = "Please enter your text: ";
					result = JOptionPane.showInputDialog(input); 
					res = result.toString();
					bottom.addLabel(res);
				}
		       });
			layout.putConstraint(SpringLayout.WEST, text,
                    200,
                    SpringLayout.WEST, container);
			layout.putConstraint(SpringLayout.NORTH, text,
                    0,
                    SpringLayout.NORTH, container);
			container.add(text);
		    
			container.setLayout(layout);
			
			
			
			
			//Delete button
			final JButton delete = new JButton();
			delete.setBorder(BorderFactory.createEmptyBorder());
			delete.setBackground(new Color(235,235,235));
			delete.setOpaque(true);
			try {
			    Image img = ImageIO.read(getClass().getResource("resources/trash.png"));
			    delete.setIcon(new ImageIcon(img));
			  } catch (IOException ex) {
			  }
			delete.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Vector<DraggableImageComponent> list = bottom.getElements();
					Vector<DraggableImageComponent> remove = new Vector<DraggableImageComponent>();
					for (DraggableImageComponent e : list){
						if(((DraggableImageComponent)e).isSelected()){
							remove.add(e);
						}
					}
					
					for (DraggableImageComponent e : remove){
						list.remove(e);
						bottom.remove(e);
					}
					
					remove = null;
					
					Vector<MyLabel> list2 = bottom.getLabels();
					Vector<MyLabel> remove2 = new Vector<MyLabel>();
					for (MyLabel e : list2){
						if(((MyLabel)e).isSelected()){
							remove2.add(e);
						}
					}
					
					for (MyLabel e : remove2){
						list2.remove(e);
						bottom.remove(e);
					}
					
					remove2 = null;
					bottom.repaint();
				}
		       });
			layout.putConstraint(SpringLayout.WEST, delete,
                    10,
                    SpringLayout.EAST, text);
			layout.putConstraint(SpringLayout.NORTH, delete,
                    0,
                    SpringLayout.NORTH, container);
			container.add(delete);
			
			//Increase button
			final JButton increase = new JButton();
			increase.setBorder(BorderFactory.createEmptyBorder());
			increase.setBackground(new Color(235,235,235));
			increase.setOpaque(true);
			try {
			    Image img = ImageIO.read(getClass().getResource("resources/font-increase.png"));
			    increase.setIcon(new ImageIcon(img));
			  } catch (IOException ex) {
			  }
			increase.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Vector<MyLabel> list = bottom.getLabels();
					for (MyLabel e : list){
						if(((MyLabel)e).isSelected()){
							e.increaseSize();
						}
					}
					
					bottom.repaint();
				}
		       });
			layout.putConstraint(SpringLayout.WEST, increase,
                    10,
                    SpringLayout.EAST, delete);
			layout.putConstraint(SpringLayout.NORTH, increase,
                    0,
                    SpringLayout.NORTH, container);
			container.add(increase);
			
			//Decrease button
			final JButton decrease = new JButton();
			decrease.setBorder(BorderFactory.createEmptyBorder());
			decrease.setBackground(new Color(235,235,235));
			decrease.setOpaque(true);
			try {
			    Image img = ImageIO.read(getClass().getResource("resources/font-decrease.png"));
			    decrease.setIcon(new ImageIcon(img));
			  } catch (IOException ex) {
			  }
			decrease.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Vector<MyLabel> list = bottom.getLabels();
					for (MyLabel e : list){
						if(((MyLabel)e).isSelected()){
							e.decreaseSize();
						}
					}
					
					bottom.repaint();
				}
		       });
			layout.putConstraint(SpringLayout.WEST, decrease,
                    10,
                    SpringLayout.EAST, increase);
			layout.putConstraint(SpringLayout.NORTH, decrease,
                    0,
                    SpringLayout.NORTH, container);
			container.add(decrease);
		
			//Print button
			final JButton print = new JButton();
			print.setBorder(BorderFactory.createEmptyBorder());
			print.setBackground(new Color(235,235,235));
			try {
			    Image img = ImageIO.read(getClass().getResource("resources/printer.png"));
			    print.setIcon(new ImageIcon(img));
			  } catch (IOException ex) {
			  }
			print.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					PrinterJob job = PrinterJob.getPrinterJob();
		        	 PageFormat pf=job.defaultPage();    
		        	 pf.setOrientation(PageFormat.LANDSCAPE);
		        	 Paper paper = pf.getPaper();  
	                 paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());  
	                 pf.setPaper(paper);
		        	 job.setPrintable(new Printer(bottom), pf);
		        	 
		        	 boolean doPrint = job.printDialog();
		        	 if (doPrint) {
		        		    try {
		        		        job.print();
		        		    } catch (PrinterException exception) {
		        		        // The job did not successfully
		        		        // complete
		        		    }
		        		}
				}
		       });
			layout.putConstraint(SpringLayout.WEST, print,
                    10,
                    SpringLayout.EAST, decrease);
			layout.putConstraint(SpringLayout.NORTH, print,
                    0,
                    SpringLayout.NORTH, container);
			container.add(print);
	    
			//ClearAll Button
			final JButton clearAll = new JButton("Clear All"); 
			//clearAll.setBorder(BorderFactory.createEmptyBorder());
			clearAll.setBackground(new Color(235,235,235));
			clearAll.setOpaque(true);
			container.add(clearAll); 
			layout.putConstraint(SpringLayout.WEST, clearAll,
                    175,
                    SpringLayout.EAST, text);
			layout.putConstraint(SpringLayout.NORTH, clearAll,
                    5,
                    SpringLayout.NORTH, container);
			
			clearAll.addActionListener(new ActionListener() {
		         
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Vector<DraggableImageComponent> list = bottom.getElements();
					
					for (DraggableImageComponent e : list){
						bottom.remove(e);
					}
					
					list.removeAllElements();
					
					Vector<MyLabel> list2 = bottom.getLabels();
					for (MyLabel e : list2){
						bottom.remove(e);
					}
					
					list2.removeAllElements();
					
					bottom.resetWord();
					bottom.resetButton();
					reading.reset();
					writing.reset();
					
					bottom.repaint();

				}
			 });
			
		
	    
	    
	    //draw top container
	    JPanel logoContainer = new JPanel();
	    logoContainer.setLayout(new FlowLayout(FlowLayout.LEFT));
	    logoContainer.setBackground(new Color(235,235,235));
	    final JButton logo = new JButton();
	    logo.setBorder(BorderFactory.createEmptyBorder());
	    logo.setBackground(new Color(235,235,235));
		try {
		    Image img = ImageIO.read(getClass().getResource("resources/logo.png"));
		    logo.setIcon(new ImageIcon(img));
		  } catch (IOException ex) {
		  }
		logo.setOpaque(true);
		logoContainer.add(logo);
		
	    
	    //initiate window layout
	    JPanel window = new JPanel();
	    window.setLayout(new BorderLayout());
	    window.add(logoContainer,BorderLayout.NORTH);
	    window.add(top,BorderLayout.WEST);
	    window.add(container,BorderLayout.CENTER);
	    
	    
	    
	    //initiate JFrame properties
	    setTitle("Conductive Ink Toolkit");
	    setSize(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT);
	    setLocationRelativeTo(null);
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
	    setResizable(true);
	    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
	    add(window);
	  }
}
